Java RMI (Remote Method Invocation) একটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশন পরিবেশে মেথড কলের মাধ্যমে ডেটা আদান-প্রদান করার সুযোগ দেয়। RMI মেথডে প্যারামিটার পাস করা হয় pass-by-value এর মাধ্যমে।
RMI মেথডের প্যারামিটার দুই প্রকারের হতে পারে:
int
, float
, boolean
। এগুলো সরাসরি pass-by-value হিসেবে পাস হয়।Serializable Interface:
প্যারামিটার হিসেবে পাঠানো অবজেক্ট Serializable হতে হবে।
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PersonService extends Remote {
String greetPerson(Person person) throws RemoteException;
}
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class PersonServiceImpl extends UnicastRemoteObject implements PersonService {
public PersonServiceImpl() throws RemoteException {
super();
}
@Override
public String greetPerson(Person person) throws RemoteException {
return "Hello, " + person.getName() + "! You are " + person.getAge() + " years old.";
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RMIServer {
public static void main(String[] args) {
try {
PersonServiceImpl personService = new PersonServiceImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("PersonService", personService);
System.out.println("RMI Server is running...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class RMIClient {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
PersonService personService = (PersonService) registry.lookup("PersonService");
// Person অবজেক্ট তৈরি করে পাস করা
Person person = new Person("Alice", 30);
String response = personService.greetPerson(person);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hello, Alice! You are 30 years old.
java.io.NotSerializableException
ত্রুটি দেখা দেয়।RMI তে প্যারামিটার পাস করার মাধ্যমে ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা আদান-প্রদান সহজ হয়। Primitive টাইপ এবং Serializable অবজেক্ট পাস করা RMI-তে সাধারণ একটি প্রক্রিয়া, যা ডিস্ট্রিবিউটেড অ্যাপ্লিকেশন তৈরিতে কার্যকর।
Java RMI (Remote Method Invocation) ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা পাস করার একটি পদ্ধতি প্রদান করে। এটি মূলত Serialization এবং Deserialization ব্যবহার করে ডেটা পাঠায়। RMI-এর মাধ্যমে ডেটা পাস করার প্রক্রিয়াটি অত্যন্ত গুরুত্বপূর্ণ, কারণ এটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশনের কম্পোনেন্টগুলোর মধ্যে কার্যকর যোগাযোগ নিশ্চিত করে।
int
, double
, boolean
, ইত্যাদি সরাসরি রিমোট মেথডের প্যারামিটার হিসেবে পাস করা যায়।java.io.Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।RMI এর মাধ্যমে প্রিমিটিভ টাইপ পাস করা সবচেয়ে সহজ। উদাহরণস্বরূপ:
public interface MyRemoteInterface extends Remote {
int addNumbers(int a, int b) throws RemoteException;
}
Server Implementation:
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemoteInterface {
public MyRemoteImpl() throws RemoteException {
super();
}
@Override
public int addNumbers(int a, int b) throws RemoteException {
return a + b;
}
}
Client:
public class RMIClient {
public static void main(String[] args) {
try {
MyRemoteInterface remoteObj = (MyRemoteInterface) Naming.lookup("rmi://localhost:1099/MyService");
int result = remoteObj.addNumbers(5, 10);
System.out.println("Result from server: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
যদি ক্লাসের অবজেক্ট RMI এর মাধ্যমে পাঠাতে হয়, তবে ক্লাসটি Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।
Example: Data Class
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int rollNumber;
public Student(String name, int rollNumber) {
this.name = name;
this.rollNumber = rollNumber;
}
public String getName() {
return name;
}
public int getRollNumber() {
return rollNumber;
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface StudentService extends Remote {
String getStudentDetails(Student student) throws RemoteException;
}
import java.rmi.server.UnicastRemoteObject;
public class StudentServiceImpl extends UnicastRemoteObject implements StudentService {
public StudentServiceImpl() throws RemoteException {
super();
}
@Override
public String getStudentDetails(Student student) throws RemoteException {
return "Student Name: " + student.getName() + ", Roll Number: " + student.getRollNumber();
}
}
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
StudentService service = (StudentService) Naming.lookup("rmi://localhost:1099/StudentService");
Student student = new Student("Alice", 101);
String details = service.getStudentDetails(student);
System.out.println("Response from Server: " + details);
} catch (Exception e) {
e.printStackTrace();
}
}
}
RMI এর মাধ্যমে Remote Object এর Reference পাস করার জন্য একটি Remote Interface এবং তার ইমপ্লিমেন্টেশন প্রয়োজন।
public interface CalculatorService extends Remote {
Calculator getCalculator() throws RemoteException;
}
public interface Calculator extends Remote {
int multiply(int a, int b) throws RemoteException;
}
Server Implementation:
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
public CalculatorImpl() throws RemoteException {
super();
}
@Override
public int multiply(int a, int b) throws RemoteException {
return a * b;
}
}
public class CalculatorServiceImpl extends UnicastRemoteObject implements CalculatorService {
public CalculatorServiceImpl() throws RemoteException {
super();
}
@Override
public Calculator getCalculator() throws RemoteException {
return new CalculatorImpl();
}
}
Client Implementation:
public class RMIClient {
public static void main(String[] args) {
try {
CalculatorService service = (CalculatorService) Naming.lookup("rmi://localhost:1099/CalculatorService");
Calculator calculator = service.getCalculator();
int result = calculator.multiply(4, 5);
System.out.println("Multiplication Result: " + result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java RMI এর মাধ্যমে প্রিমিটিভ টাইপ, Serializable Object, এবং Remote Object Reference পাস করা যায়। এটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশনে সহজ ও কার্যকর ডেটা পাসিং নিশ্চিত করে। সঠিক Serialization ও Security ব্যবস্থার মাধ্যমে RMI ডেটা পাসিং আরও নির্ভরযোগ্য এবং কার্যকর হয়।
Java RMI (Remote Method Invocation) প্রযুক্তি ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা আদান-প্রদানের সুবিধা প্রদান করে। এই ডেটা হতে পারে Primitive Data Types যেমন int
, double
, boolean
, অথবা Complex Data Types যেমন অবজেক্ট, অ্যারে, বা কাস্টম ক্লাস।
int
, float
, char
, boolean
, ইত্যাদি।Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
double multiply(double x, double y) throws RemoteException;
}
import java.rmi.server.UnicastRemoteObject;
public class CalculatorImpl extends UnicastRemoteObject implements Calculator {
protected CalculatorImpl() throws RemoteException {
super();
}
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
@Override
public double multiply(double x, double y) throws RemoteException {
return x * y;
}
}
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
Calculator calculator = (Calculator) Naming.lookup("rmi://localhost:1099/CalculatorService");
int sum = calculator.add(5, 10);
double product = calculator.multiply(3.5, 2.0);
System.out.println("Sum: " + sum);
System.out.println("Product: " + product);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{name='" + name + "', age=" + age + "}";
}
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface StudentService extends Remote {
String getStudentDetails(Student student) throws RemoteException;
}
import java.rmi.server.UnicastRemoteObject;
public class StudentServiceImpl extends UnicastRemoteObject implements StudentService {
protected StudentServiceImpl() throws RemoteException {
super();
}
@Override
public String getStudentDetails(Student student) throws RemoteException {
return "Received: " + student.toString();
}
}
import java.rmi.Naming;
public class RMIClient {
public static void main(String[] args) {
try {
StudentService studentService = (StudentService) Naming.lookup("rmi://localhost:1099/StudentService");
Student student = new Student("John Doe", 20);
String response = studentService.getStudentDetails(student);
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।উদাহরণ:
public class Student implements Serializable {
private String name;
private int age;
}
বৈশিষ্ট্য | Primitive Data Types | Complex Data Types |
---|---|---|
Serialization প্রয়োজন | প্রয়োজন নেই | প্রয়োজন |
ডেটা টাইপ | int , float , boolean , ইত্যাদি। | কাস্টম ক্লাস, অবজেক্ট, লিস্ট, অ্যারে। |
পারফরম্যান্স | দ্রুত এবং কম জটিল | তুলনামূলকভাবে ধীর (Serialization কারণে)। |
ব্যবহারিক ক্ষেত্র | সাধারণ গাণিতিক কাজ বা স্ট্যাটিক ডেটা পাস করা। | জটিল অবজেক্ট বা ডেটা স্ট্রাকচার পাস করা। |
Java RMI-তে Primitive Data Types সরাসরি এবং দ্রুত পাস করা যায়, যেখানে Complex Data Types পাস করার জন্য Serialization এবং Deserialization প্রয়োজন। Complex Data Types ব্যবহারে Serializable
ইন্টারফেস ব্যবহার করা বাধ্যতামূলক। এই পদ্ধতিগুলি RMI প্রযুক্তিকে অত্যন্ত কার্যকর এবং শক্তিশালী করে তোলে।
RMI-তে ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা পাঠানোর জন্য Object Serialization একটি গুরুত্বপূর্ণ ভূমিকা পালন করে। এটি একটি প্রক্রিয়া যার মাধ্যমে একটি অবজেক্টকে এমন ফরম্যাটে রূপান্তর করা হয় যা নেটওয়ার্কের মাধ্যমে স্থানান্তরযোগ্য এবং পরবর্তীতে পুনরায় ডি-সিরিয়ালাইজ করা যায়।
Serialization হলো একটি প্রক্রিয়া, যার মাধ্যমে Java অবজেক্টকে byte stream এ রূপান্তর করা হয়। এটি পরবর্তীতে ফাইল, ডেটাবেস, বা নেটওয়ার্কের মাধ্যমে স্থানান্তরের জন্য ব্যবহৃত হয়।
Deserialization হলো বিপরীত প্রক্রিয়া, যা byte stream থেকে অবজেক্ট পুনরুদ্ধার করে।
RMI-তে ক্লায়েন্ট এবং সার্ভারের মধ্যে যোগাযোগের সময়, Serializable অবজেক্টগুলো নেটওয়ার্কের মাধ্যমে পাঠানো হয়।
Serializable
Interface:java.io.Serializable
ইন্টারফেস ইমপ্লিমেন্ট করতে হবে।import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L; // Unique ID for Serialization
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + '}';
}
// Getters and Setters
}
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PersonService extends Remote {
String savePerson(Person person) throws RemoteException;
Person getPerson() throws RemoteException;
}
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class PersonServiceImpl extends UnicastRemoteObject implements PersonService {
private Person person;
protected PersonServiceImpl() throws RemoteException {
super();
}
@Override
public String savePerson(Person person) throws RemoteException {
this.person = person;
return "Person saved: " + person;
}
@Override
public Person getPerson() throws RemoteException {
return this.person;
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Server {
public static void main(String[] args) {
try {
PersonService personService = new PersonServiceImpl();
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("PersonService", personService);
System.out.println("Server is running...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
PersonService personService = (PersonService) registry.lookup("PersonService");
// Sending a serialized object
Person person = new Person("John Doe", 30);
String response = personService.savePerson(person);
System.out.println(response);
// Retrieving the object
Person retrievedPerson = personService.getPerson();
System.out.println("Retrieved Person: " + retrievedPerson);
} catch (Exception e) {
e.printStackTrace();
}
}
}
transient
বা static
ফিল্ডগুলো সিরিয়ালাইজ হয় না।InvalidClassException
হতে পারে যদি serialVersionUID
ঠিকমতো সেট না থাকে।transient
ডিক্লেয়ার করতে হবে।serialVersionUID
ব্যবহার করা হয়।উদাহরণ | Serialization Type | ব্যাখ্যা |
---|---|---|
Remote Object | Reference | ক্লায়েন্ট Remote Object এর রেফারেন্স পায় এবং মেথড কল করে। |
Custom Object | Value | অবজেক্টের কপি পাঠানো হয় এবং নেটওয়ার্কের মাধ্যমে স্থানান্তরিত হয়। |
RMI-তে Object Serialization এর মাধ্যমে ক্লায়েন্ট এবং সার্ভারের মধ্যে জটিল ডেটা সহজেই স্থানান্তর করা যায়। এটি জাভা ভিত্তিক ডিস্ট্রিবিউটেড সিস্টেম গঠনে একটি গুরুত্বপূর্ণ ভূমিকা পালন করে। Serialization ব্যবহারের সময় পারফরম্যান্স এবং নিরাপত্তা বিষয়েও সতর্ক থাকা জরুরি।
Java RMI (Remote Method Invocation) প্রযুক্তি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশনের জন্য ব্যবহার করা হয় যেখানে ক্লায়েন্ট এবং সার্ভার একে অপরের সাথে দূরবর্তী অবস্থান থেকে যোগাযোগ করতে পারে। RMI-এর মাধ্যমে ক্লায়েন্ট রিমোট সার্ভারে মেথড কল করতে পারে, এবং সেই মেথডে প্যারামিটার পাস করতে এবং রিটার্ন ভ্যালু গ্রহণ করতে পারে।
import java.rmi.Remote;
import java.rmi.RemoteException;
// Remote Interface
public interface MathService extends Remote {
// Add two numbers
int add(int a, int b) throws RemoteException;
// Find maximum of two numbers
int max(int a, int b) throws RemoteException;
// Concatenate two strings
String concatenate(String str1, String str2) throws RemoteException;
}
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
// Remote Object Implementation
public class MathServiceImpl extends UnicastRemoteObject implements MathService {
protected MathServiceImpl() throws RemoteException {
super();
}
@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
@Override
public int max(int a, int b) throws RemoteException {
return Math.max(a, b);
}
@Override
public String concatenate(String str1, String str2) throws RemoteException {
return str1 + str2;
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
// RMI Server
public class Server {
public static void main(String[] args) {
try {
// Create and export the remote object
MathService mathService = new MathServiceImpl();
// Bind the remote object to the registry
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("MathService", mathService);
System.out.println("Server is running and MathService is ready...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
// RMI Client
public class Client {
public static void main(String[] args) {
try {
// Locate the RMI registry
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
// Lookup the remote object
MathService mathService = (MathService) registry.lookup("MathService");
// Call remote methods and collect results
int sum = mathService.add(10, 20);
int maximum = mathService.max(30, 25);
String concatenated = mathService.concatenate("Hello, ", "World!");
// Print results
System.out.println("Addition (10 + 20): " + sum);
System.out.println("Maximum (30, 25): " + maximum);
System.out.println("Concatenation: " + concatenated);
} catch (Exception e) {
e.printStackTrace();
}
}
}
add(int a, int b)
মেথডে দুটি সংখ্যা পাস করা হয়।max(int a, int b)
মেথডে দুটি সংখ্যা পাস করা হয়।concatenate(String str1, String str2)
মেথডে দুটি স্ট্রিং পাস করা হয়।add
মেথড থেকে একটি সংখ্যা রিটার্ন হয়।max
মেথড থেকে সর্বাধিক সংখ্যা রিটার্ন হয়।concatenate
মেথড থেকে দুটি স্ট্রিং একত্রিত করে রিটার্ন হয়।সার্ভার এবং ক্লায়েন্ট কোড কম্পাইল করুন:
javac *.java
RMI Server চালু করুন:
java Server
RMI Client চালু করুন:
java Client
Server is running and MathService is ready...
Addition (10 + 20): 30
Maximum (30, 25): 30
Concatenation: Hello, World!
Java RMI-তে Parameters Passing এবং Return Value Management সহজ, স্বচ্ছ, এবং টাইপ সেফ। Serializable প্যারামিটার এবং রিটার্ন ভ্যালু ব্যবহার করে ক্লায়েন্ট এবং সার্ভার নির্বিঘ্নে যোগাযোগ করতে পারে। উদাহরণটি ডিস্ট্রিবিউটেড অ্যাপ্লিকেশন তৈরির জন্য একটি শক্তিশালী ভিত্তি দেয়।
Read more